// pages/calendar/calendar.js
const app = getApp()
var host = app.globalData.host;
var self = null;
const MONTHS = ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"];
const WEEKS = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"];

Page({
  data: {
    //tabbar
    tabbar: {},
    isIphoneX: app.globalData.isIphoneX,
    showAuth: false,
    
    themeColor: '',
    list: [],
    keyword: '',
    page: 1,
    size: 0,
    total: 0,
    currentIndex: 0,

    currentDate: null,
    currentYear: '',
    currentMonth: '',
    selectedDay0: null,
    selectedDay1: null,
    yearOfToday: new Date().getFullYear(),
    monthOfToday: new Date().getMonth(),
    dayOfToday: new Date().getDate(),

    months: MONTHS,
    weeks: WEEKS,
    days: []
  },
  onLoad: function () {
    //隐藏系统tabbar
    wx.hideTabBar();
    //tabbar调用
    app.editTabbar();
    
    
    self = this;
    self.getStyle();
    self.loadCalendar(new Date);
  },
  onShow: function () {
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          self.init();
        } else {
          wx.setStorageSync("path", self.route);
          // wx.reLaunch({
          //   url: '/pages/login/login'
          // })
          self.setData({
            showAuth: true
          })
        }
      }
    })
  },
  getStyle() {
    self.setData({
      themeColor: app.globalData.themeColor
    })
  },
  init() {
    var url = host + '/yq/item/calendar';
    var data = {};
    // if (self.data.selectedDay0 !== null && self.data.selectedDay1 !== null) {
    //   var startTime = self.data.currentYear + '-' + self.timeFormat(self.data.currentMonth + 1) + '-' + self.timeFormat(self.data.selectedDay0) + ' 00:00:00';
    //   var endTime = self.data.currentYear + '-' + self.timeFormat(self.data.currentMonth + 1) + '-' + self.timeFormat(self.data.selectedDay1) + ' 23:59:59';
    //   data.startTime = startTime;
    //   data.endTime = endTime;
    // }

    // if (self.data.selectedDay0 !== null) {
    //   var startTime = self.data.currentYear + '-' + self.timeFormat(self.data.currentMonth + 1) + '-' + self.timeFormat(self.data.selectedDay0) + ' 00:00:00';
    //   var endTime = self.data.currentYear + '-' + self.timeFormat(self.data.currentMonth + 1) + '-' + self.timeFormat(self.data.selectedDay0) + ' 23:59:59';
    //   data.startTime = startTime;
    //   data.endTime = endTime;
    // }

    data.date = self.data.currentYear + '-' + self.timeFormat(self.data.currentMonth + 1) + '-01';
    
    var success = function (res) {
      var list = res.info;
      self.setData({
        list: list,
        page: res.page,
        size: res.size,
        total: res.total
      })
    };
    var fail = function (res) { };
    var complete = function (res) { };
    app.request(url, data, success, fail, complete);
  },
  loadCalendar(date) {
    var firstDayOfMonth = new Date;
    firstDayOfMonth.setFullYear(date.getFullYear());
    firstDayOfMonth.setMonth(date.getMonth());
    firstDayOfMonth.setDate(1);
    var year = firstDayOfMonth.getFullYear();
    var month = firstDayOfMonth.getMonth();
    var week = firstDayOfMonth.getDay();
    var days = new Array;
    var dayCount = 0;
    if (month == 0 || month == 2 || month == 4 || month == 6 || month == 7 || month == 9 || month == 11) {
      dayCount = 31;
    } else if (month == 1 && year % 4 == 0) {
      dayCount = 29;
    } else if (month == 1 && year % 4 != 0) {
      dayCount = 28;
    } else {
      dayCount = 30;
    }
    for (var i = 0; i < week; i ++) { // 第一行留空
      days.push({
        date: ""
      });
    }
    for (var i = 0; i < dayCount; i ++) {
      days.push({
        year: firstDayOfMonth.getFullYear(),
        month: firstDayOfMonth.getMonth(),
        date: i + 1
      });
    }

    self.setData({
      currentDate: date,
      currentYear: date.getFullYear(),
      currentMonth: date.getMonth(),
      selectedDay0: null,
      selectedDay1: null,
      days: days
    })
  },
  prevMonth(e) {
    var newDate = self.data.currentDate;
    newDate.setMonth(self.data.currentDate.getMonth() - 1);
    self.loadCalendar(newDate);

    self.init();
  },
  nextMonth(e) {
    var newDate = self.data.currentDate;
    newDate.setMonth(self.data.currentDate.getMonth() + 1);
    self.loadCalendar(newDate);

    self.init();
  },
  selectDay(e) {
    return;
    if (!e.currentTarget.dataset.day) {
      return;
    }
    var day = Number(e.currentTarget.dataset.day);
    // if ((self.data.selectedDay0 === null && self.data.selectedDay1 === null)
    //   || (self.data.selectedDay0 !== null && self.data.selectedDay1 !== null)) {
    //   self.setData({
    //     selectedDay0: day,
    //     selectedDay1: null
    //   })
    // } else if (self.data.selectedDay0 !== null && self.data.selectedDay1 === null) {
    //   if (day < self.data.selectedDay0) {
    //     self.setData({
    //       selectedDay1: self.data.selectedDay0
    //     })
    //     self.setData({
    //       selectedDay0: day
    //     })
    //   } else {
    //     self.setData({
    //       selectedDay1: day
    //     })
    //   }
    //   self.init();
    // }
    self.setData({
      selectedDay0: day
    })
    self.init();
  },
  expand(e) {
    var index = e.currentTarget.dataset.index;
    var itemId = e.currentTarget.dataset.itemid;
    // if (Number(index) == self.data.currentIndex) {
      wx.navigateTo({
        url: '/pages/productInfo/productInfo?itemId=' + itemId
      })
      // return;
    // }
    // self.setData({
    //   currentIndex: Number(index)
    // })
  },
  invite(e) {
    var productId = e.currentTarget.dataset.productid;
    wx.navigateTo({
      url: '/pages/pintuan/pintuan?productId=' + productId
    })
  },
  onShareAppMessage(object) {
    console.log(object);
    if (object.from == "button") {
      var itemId = object.target.dataset.itemid;
      var shareTitle = object.target.dataset.sharetitle;
      var shareImage = object.target.dataset.shareimage;
      return {
        title: shareTitle,
        imageUrl: shareImage,
        path: "/pages/productInfo/productInfo?itemId=" + itemId
      }
    } else {
      return null;
    }
  },
  showQRCode(e) {
    var url = e.currentTarget.dataset.url;
    wx.previewImage({
      urls: [url]
    })
  },
  none(e) {
    return;
  },
  timeFormat(param) {
    return param < 10 ? '0' + param : param;
  }
})
